package org.hxd.admin.order.controller;

import org.hxd.admin.order.service.AdminOrderService;
import org.hxd.pojo.Orderitem;
import org.hxd.pojo.Orders;
import org.hxd.utils.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/admin/orders")
public class AdminOrderController {
    @Autowired
    AdminOrderService adminOrderService;
    @RequestMapping("/findorders")
    public String findorders(Orders orders, Model model,@RequestParam(defaultValue = "1") Integer pageIndex){
        PageModel pageModel = new PageModel();
        pageModel.setPageIndex(pageIndex);
        pageModel.setPageSize(8);
        int count = adminOrderService.findOrderTjcount(orders);
        pageModel.setRocordCount(count);
        List<Orders> ordersList = adminOrderService.findOrderTj(orders,pageModel);
        model.addAttribute("orders",ordersList);
        model.addAttribute("o",orders);
        model.addAttribute("pageModel",pageModel);
        return "admin/orders/list";
    }

    //删除已支付订单
    @RequestMapping("/deleteOrderByib")
    @ResponseBody
    public String deleteOrderByib(String id){
        int rows = adminOrderService.deleteOrderByib(id);
        if(rows > 0){
            return "OK";
        }else {
            return "FAIL";
        }
    }

    //根据id查找Order
    @RequestMapping("/findOrderById")
    public String findOrderById(String id, Model model){
        Orders orders = adminOrderService.findOrderById(id);
        System.out.println(orders);
        List<Orderitem> orderitems = adminOrderService.findOrderitemById(orders.getId());
        model.addAttribute("order",orders);
        model.addAttribute("orderitems",orderitems);
        return "admin/orders/view";
    }
}
